package com.example.zx.theline.usercenter;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.zx.theline.R;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import GsonBean.UserBean;
import app.MyApplication;
import base.BaseActivity;
import greendao.entity.User;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import tools.MyToast;

public class UserCenterActivity extends BaseActivity {

    Button mUpUserBean,mCleanUserBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_center);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {
        mUpUserBean= (Button) findViewById(R.id.upuserinfo);
        mCleanUserBean= (Button) findViewById(R.id.cleanuserinfo);
    }

    @Override
    protected void InitData() {

    }

    @Override
    protected void InitListen() {
        //根据ID删除所有本地单位人员信息
        mCleanUserBean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        //更新ID更新本地数据库，该ID部门下的所有人员信息
        mUpUserBean.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              RequestParams mRequestParams=new RequestParams();
              mRequestParams.add("ljyyxDwxxId","102");
              NetWorkApi.getAllUserInfoByCompanyId(mRequestParams, new ObjectHttpResponseHandler(UserCenterActivity.this, UserBean.class) {
                  @Override
                  public void Success(int i, Header[] headers, Object object) {

                    UserBean mUserBean= (UserBean) object;
                      List<User> list=new ArrayList<User>();
                      MyApplication.getDaoSession().getUserDao().deleteAll();
                      for (UserBean.RowsBean mRowsBean:mUserBean.getRows()){
                          User mUser=new User();
                          mUser.setLjyyxLoginName(mRowsBean.getLjyyxLoginName());
                          mUser.setLjyyxWorkerName(mRowsBean.getLjyyxWorkerName());
                          mUser.setLjyyxWorkerTel(mRowsBean.getLjyyxWorkerTel());
                          mUser.setDownUnitName(mRowsBean.getDownUnitName());
                          mUser.setLjyyxDwxxId(mRowsBean.getLjyyxDwxxId());
                          list.add(mUser);
                      }
                      MyApplication.getDaoSession().getUserDao().insertOrReplaceInTx(list);
                      MyToast.showToast("数据库更新成功");
                  }
                  @Override
                  public void Failure(int i, Header[] headers, String s, Throwable throwable) {
                  }
              });
          }
      });
    }
}
